/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.mine.presenter;

import android.text.TextUtils;

import com.suozhang.framework.entity.bo.PageData;
import com.yiqi.liebang.entity.bo.AddTopicBo;
import com.yiqi.liebang.entity.bo.CouponBo;
import com.yiqi.liebang.entity.bo.PageBo;
import com.yiqi.liebang.feature.mine.contract.CouponContract;
import com.yiqi.liebang.feature.mine.contract.TopicContract;
import com.yiqi.liebang.feature.mine.dependencies.coupon.DaggerCouponPresenterComponent;
import com.yiqi.liebang.feature.mine.dependencies.topic.DaggerTopicPresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class CouponPresenter implements CouponContract.Presenter {

    private CouponContract.View mView;

    @Inject

    CouponContract.Model mModel;


    public CouponPresenter(CouponContract.View mView) {
        this.mView = mView;
        DaggerCouponPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void getCoupon(int type, int pageIndex, int pageSize,String mLabelsStr) {
        mModel.getCoupon(new PageBo(pageIndex, pageSize, type,mLabelsStr)).compose(mView.bindToLife()).subscribe(new Observer<List<CouponBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<CouponBo> couponBoPageData) {
                mView.dismissLoading();
                if (couponBoPageData!=null&&couponBoPageData.size()>0){
                    mView.showCouponSuccess(couponBoPageData);
                }else {
                    mView.showCouponEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showCouponError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getCouponMore(int type, int pageIndex, int pageSize,String mLabelsStr) {
        mModel.getCoupon(new PageBo(pageIndex, pageSize, type, mLabelsStr)).compose(mView.bindToLife()).subscribe(new Observer<List<CouponBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<CouponBo> couponBoPageData) {
              mView.showCouponMoreSuccess(couponBoPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showCouponMoreError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
}
